#include "../include/mysql.h"
#include "../include/boss.h"
#include "../include/employee.h"
#include "../include/manager.h"
#include <mysql/mysql.h>
#include <string.h>

MysqlManagement::MysqlManagement() { db = mysql_init(NULL); }

int MysqlManagement::connectDB() {
  const char *host = "localhost";
  const char *user = "root";
  const char *password = "123";
  const char *database = "CPPOBJS";

  MYSQL *ret =
      mysql_real_connect(this->db, host, user, password, database, 0, NULL, 0);

  if (ret == NULL) {
    printf("Error:%s\n", mysql_error(this->db));
    return -1;
  }

  return 0;
}

// 是否在数据库中
bool MysqlManagement::isInDB(int id) {
  char sql[1024];
  sprintf(sql, "select * from worker where id = '%d';", id);
  int qret = mysql_query(this->db, sql);
  if (qret != 0) {
    printf("IsinDB,error:%s\n", mysql_error(this->db));
    return 0;
  }

  MYSQL_RES *result = mysql_store_result(db);
  int num = mysql_num_rows(result);
  if (num == 0) {
    return 0;
  }
  return 1;
}

int MysqlManagement::addDB(Worker *newWorker) {
  if (!isInDB(newWorker->w_ID)) {
    char sql[1024];
    sprintf(sql, "insert into worker (id,name,deptId) VALUES ('%d','%s','%d');",
            newWorker->w_ID, newWorker->w_Name, newWorker->w_DeptId);
    int qret = mysql_query(this->db, sql);
    if (qret != 0) {
      printf("Error:%s\n", mysql_error(this->db));
      return -1;
    }

    return 1;
  } else {
    cout << "该编号已经存在！请重新输入！" << endl;
    return 0;
  }
}

// 打印员工信息
int MysqlManagement::showDB(MYSQL_RES *result) {
  MYSQL_ROW row;
  while ((row = mysql_fetch_row(result)) != NULL) {
    Worker *worker = NULL;
    if (strcmp(row[2], "1") == 0) {
      worker = new Boss(atoi(row[0]), row[1], atoi(row[2]));
      worker->showInfo();
      delete worker;
    } else if (strcmp(row[2], "2") == 0) {
      worker = new Manager(atoi(row[0]), row[1], atoi(row[2]));
      worker->showInfo();
      delete worker;
    } else if (strcmp(row[2], "3") == 0) {
      worker = new Employee(atoi(row[0]), row[1], atoi(row[2]));
      worker->showInfo();
      delete worker;
    }
  }
  return 0;
}

int MysqlManagement::deleteDB(int number) {

  if (!isInDB(number)) {
    cout << "您查询的员工并不存在，请重新输入！" << endl;
    return 0;
  }

  char sql[1024];
  sprintf(sql, "delete from worker where id = '%d';", number);
  int qret = mysql_query(this->db, sql);
  if (qret != 0) {
    cout << "删除失败！" << endl;
    return 0;
  }

  cout << "删除成功！" << endl;
  return 1;
}

int MysqlManagement::alterDB(int id, Worker *updatedWorker) {
  char sql[1024];

  if (this->isInDB(updatedWorker->w_ID)) {
    cout << "新的编号已经存在，请重新输入！" << endl;
    return 0;
  }

  sprintf(
      sql,
      "update worker set id = '%d',name = '%s',deptId = '%d' where id = '%d';",
      updatedWorker->w_ID, updatedWorker->w_Name, updatedWorker->w_DeptId, id);

  int qret = mysql_query(this->db, sql);
  if (qret != 0) {
    cout << "更新失败！" << endl;
    return 0;
  }

  cout << "更新成功！" << endl;
  return 1;
}

int MysqlManagement::clearDB() {
  char sql[1024];
  sprintf(sql, "delete from worker;");

  int ret = mysql_query(this->db, sql);
  if (ret != 0) {
    cout << "清空失败！" << endl;
  }

  cout << "清空成功！" << endl;
  return 1;
}
